﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using System.Diagnostics;
using XJLSC.PO;
using System.IO;
using System.Data.OleDb;
using System.Web;
using Microsoft.CSharp;
using FastReport;
using FastReport.Utils;
using System.Reflection;
using System.Net;
using XJLSC.DataClass;
using Newtonsoft.Json;
using XJLSC.PM;
using XJLSC.Sy;

namespace SMALLERP.PO
{

    public partial class AppPO : Form
    {
        private const int OLDOFFICEVESION = -4143;
        private const int NEWOFFICEVESION = 56;
        DataBase db = new DataBase();
        private SqlDataReader sdr1;
        private string _cmbvalue = string.Empty;
        public delegate void WeiTuo(string ms);
        //定义委托对象
        public WeiTuo tuo;
        //GYSKC f;
        CHAXUN k;
       // OAlink g;
        private Report FReport;
        private DataSet FDataSet;
        private DataSet FDataSet1;
        private string FReportsFolder;
        private bool FReportRunning;


        public AppPO()
        {
            InitializeComponent();
        }

        public List<String> XMH
        {
            get
            {
                return _XMH;
            }
        }
        List<String> _XMH = new List<string>();

        public List<String> GDDB
        {
            get
            {
                return _GDDB;
            }
        }
        List<String> _GDDB = new List<string>();

        public List<String> GDDH
        {
            get
            {
                return _GDDH;
            }
        }
        List<String> _GDDH = new List<string>();

        public List<String> CPPH
        {
            get
            {
                return _CPPH;
            }
        }
        List<String> _CPPH = new List<string>();

        public List<String> CPPM
        {
            get
            {
                return _CPPM;
            }
        }
        List<String> _CPPM = new List<string>();

        public List<String> CPGG
        {
            get
            {
                return _CPGG;
            }
        }
        List<String> _CPGG = new List<string>();

        public List<string> CPSL
        {
            get
            {
                return _CPSL;
            }
        }
        List<string> _CPSL = new List<string>();

        public List<string> JHWG
        {
            get
            {
                return _JHWG;
            }
        }
        List<string> _JHWG = new List<string>();

        public List<string> YJWG
        {
            get
            {
                return _YJWG;
            }
        }
        List<string> _YJWG = new List<string>();

        public List<string> YQDL
        {
            get
            {
                return _YQDL;
            }
        }
        List<string> _YQDL = new List<string>();

        public List<string> ZZQRJQA
        {
            get
            {
                return _ZZQRJQA;
            }
        }
        List<string> _ZZQRJQA = new List<string>();

        public List<string> ZJPH
        {
            get
            {
                return _ZJPH;
            }
        }
        List<string> _ZJPH = new List<string>();

        public List<string> ZJPM
        {
            get
            {
                return _ZJPM;
            }
        }
        List<string> _ZJPM = new List<string>();

        public string GZZX
        {
            get
            {
                return _GZZX;
            }
        }
        String _GZZX;

        public string ZXMC
        {
            get
            {
                return _ZXMC;
            }
        }
        String _ZXMC;

        public delegate void CheckBoxClickedHandler(bool state);
        public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
        {
            bool _bChecked;
            public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
            {
                _bChecked = bChecked;
            }
            public bool Checked
            {
                get { return _bChecked; }
            }
        }
        class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
        {
            System.Drawing.Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            System.Drawing.Point _cellLocation = new System.Drawing.Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event CheckBoxClickedHandler OnCheckBoxClicked;

            public DatagridViewCheckBoxHeaderCell()
            {
            }

            protected override void Paint(System.Drawing.Graphics graphics,
                System.Drawing.Rectangle clipBounds,
                System.Drawing.Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates dataGridViewElementState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                    dataGridViewElementState, value,
                    formattedValue, errorText, cellStyle,
                    advancedBorderStyle, paintParts);
                System.Drawing.Point p = new System.Drawing.Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                    (cellBounds.Width / 2) - (s.Width / 2);
                p.Y = cellBounds.Location.Y +
                    (cellBounds.Height / 2) - (s.Height / 2);
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                        CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }

            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
            {
                System.Drawing.Point p = new System.Drawing.Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
                if (p.X >= checkBoxLocation.X && p.X <=
                    checkBoxLocation.X + checkBoxSize.Width
                && p.Y >= checkBoxLocation.Y && p.Y <=
                    checkBoxLocation.Y + checkBoxSize.Height)
                {
                    _checked = !_checked;
                    if (OnCheckBoxClicked != null)
                    {
                        OnCheckBoxClicked(_checked);
                        this.DataGridView.InvalidateCell(this);
                    }

                }
                base.OnMouseClick(e);
            }
        }


        private void abHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态

            dataGridView4.EndEdit();
            dataGridView4.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void bbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态

            rowMergeView1.EndEdit();
            rowMergeView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态

            dataGridView6.EndEdit();
            dataGridView6.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        private void cdHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态

            //dataGridView1.EndEdit();
            //dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

        //private void 采购订单_Load(object sender, EventArgs e)
        //{
        //    string strSql = null;

        //    strSql = "SELECT TD001,TD002,TD003,TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,TC003,PURTD.UDF08 AS PCH,MA002,PURTD.UDF58 AS YTSL ";
        //    strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=TC001 AND TD002=TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON TC004=MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
        //    strSql += "AND TD018='Y' AND (TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%')";

        //    try
        //    {
        //        dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message, "软件提示");
        //        throw ex;
        //    }
        //}



        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txtTH_TextChanged(object sender, EventArgs e)
        {
            //string strSql = null;

            //strSql = "SELECT TD001,TD002,TD003,RTRIM(TD004) TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,PURTC.TC003 AS TC003,PURTD.UDF08 AS PCH,PURMA.MA002 AS MA002,PURTD.UDF58 AS YTSL,TD021,COPMA.MA002 AS KHJC,TD013,TD023 ";
            //strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=PURTC.TC001 AND TD002=PURTC.TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON PURTC.TC004=PURMA.MA001 LEFT JOIN COPTC ON TD021=COPTC.TC002 LEFT JOIN COPMA ON COPTC.TC004=COPMA.MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
            //strSql += "AND TD018='Y' AND (PURTC.TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%') AND PURMA.MA002 LIKE '%" + txtGYS.Text.Trim() + "%' ORDER BY TD012";

            //try
            //{
            //    dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";
            string filename = "";
            strSql = "SELECT RTRIM(UDF07) AS 文件名 FROM PURTP WHERE TP001='" + dataGridView5.CurrentRow.Cells["BTP001"].Value.ToString() + "' AND TP002='" + dataGridView5.CurrentRow.Cells["BTP002"].Value.ToString() + "' AND TP003='" + dataGridView5.CurrentRow.Cells["BTP003"].Value.ToString() + "'";
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {
                    filename = sdr1["文件名"].ToString();
                }
                if (sdr1.HasRows == false)
                {
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
                sdr1.Close();
            }

            try
            {
                System.Net.WebClient client = new System.Net.WebClient();
                byte[] data = client.DownloadData("http://192.168.0.25:8080/Upload/Purchase/" + filename);

                //一个真正存放数据的地址，一般我们将连接存在数据库中，数据存放在数据服务器上           
                //如果网站没有什么限制的话，这样就能得到网站的图片数据了            

                string path = Application.StartupPath;
                FileStream fs = new FileStream(path + "\\" + filename, FileMode.Create);
                //将byte数组写入文件中            
                fs.Write(data, 0, data.Length);
                fs.Close();
                System.Diagnostics.Process.Start(path + "\\" + filename);
            }
            catch
            {
                MessageBox.Show("没有附件！","系统提示");
            }


        }


        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {

            //if (dataGridView1.CurrentCell == null)
            //{
            //    return;
            //}

            //string strSql = null;
            //string ss = (dataGridView1["MA002", dataGridView1.CurrentCell.RowIndex].Value == null) ? "" : dataGridView1["MA002", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //if (ss != "")
            //{
            //    strSql = "select RTRIM(UDF01) UDF01 FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF01,'')<>''";
            //    strSql += " UNION ALL select RTRIM(UDF02) FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF02,'')<>''";
            //    strSql += " UNION ALL select RTRIM(UDF03) FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF03,'')<>''";
            //    strSql += " UNION ALL select RTRIM(UDF01)+'A' UDF01 FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF01,'')<>''";
            //    strSql += " UNION ALL select RTRIM(UDF02)+'A' FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF02,'')<>''";
            //    strSql += " UNION ALL select RTRIM(UDF03)+'A' FROM PURMA  WHERE MA002='" + ss + "' AND ISNULL(UDF03,'')<>''";

            //    try
            //    {
            //        System.Data.DataTable dt;
            //        dt = db.GetDataSet1(strSql, "DSCMA").Tables["DSCMA"];
            //        comboBox2.ValueMember = "UDF01";
            //        comboBox2.DisplayMember = "UDF01";
            //        comboBox2.DataSource = dt;
            //        comboBox2.SelectedIndex = 0;
            //    }

            //    catch
            //    {

            //    }
           // }
            //strSql = "SELECT XA002,XA009,XA011,XA023 FROM COPXA WHERE XA003='" + dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND ";
            //strSql += "XA004='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA035='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";

            //try
            //{
            //    rowMergeView1.DataSource = db.GetDataSet(strSql, "COPXA").Tables["COPXA"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}

            //string cgdb = (dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value == null) ? "" : dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //string cgdh = (dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value == null) ? "" : dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //string cgxh = (dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value == null) ? "" : dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //strSql = "SELECT TG003,TH007 FROM PURTH LEFT JOIN PURTG ON TH001=TG001 AND TH002=TG002 WHERE TH011='" + cgdb + "' AND ";
            //strSql += "TH012='" + cgdh + "' AND TH013='" + cgxh + "'";

            //try
            //{
            //    dataGridView3.DataSource = db.GetDataSet(strSql, "PURTH").Tables["PURTH"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tabPage1_Click_1(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            //if (MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo).ToString() != "Yes")
            //{
            //    try
            //    {
            //        return;
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message, "软件提示");
            //        throw ex;
            //    }
            //    finally
            //    {
            //    }
            //}

            //if (rowMergeView1["XA023", rowMergeView1.CurrentCell.RowIndex].Value.ToString() == "Y")
            //{
            //    try
            //    {
            //        MessageBox.Show("已经入库的箱号不能删除！", "软件提示");
            //        return;
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message, "软件提示");
            //        throw ex;
            //    }
            //    finally
            //    {
            //    }
            //}

            //string strSql = null;
            //strSql = "DELETE FROM COPXA WHERE XA002='" + rowMergeView1["XA002", rowMergeView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA003='" + dataGridView1["TD001", rowMergeView1.CurrentCell.RowIndex].Value.ToString() + "' AND  XA004='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA035='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //try
            //{
            //    if (db.ExecDataBySql(strSql) > 0)
            //    {

            //        strSql = "SELECT XA002,XA009,XA011,XA023 FROM COPXA WHERE XA003='" + dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND ";
            //        strSql += "XA004='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND XA035='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";

            //        try
            //        {
            //            rowMergeView1.DataSource = db.GetDataSet(strSql, "COPXA").Tables["COPXA"];
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        MessageBox.Show("删除成功！", "软体提示");
            //        strSql = "SELECT TD001,TD002,TD003,RTRIM(TD004) TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,PURTC.TC003 AS TC003,PURTD.UDF08 AS PCH,PURMA.MA002 AS MA002,PURTD.UDF58 AS YTSL,TD021,COPMA.MA002 AS KHJC,TD013,TD023 ";
            //        strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=PURTC.TC001 AND TD002=PURTC.TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON PURTC.TC004=PURMA.MA001 LEFT JOIN COPTC ON TD021=COPTC.TC002 LEFT JOIN COPMA ON COPTC.TC004=COPMA.MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
            //        strSql += "AND TD018='Y' AND (PURTC.TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%') AND PURMA.MA002 LIKE '%" + txtGYS.Text.Trim() + "%'  AND TD021 LIKE '%" + textBox1.Text.Trim() + "%' ORDER BY TD012";

            //        try
            //        {
            //            dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
            //        }
            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }

            //    }
            //    else
            //    {
            //        MessageBox.Show("删除失败！", "软体提示");
            //        return;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软体提示");
            //    throw ex;
            //}

        }

        public void AppPO_Activated(object sender, EventArgs e)
        {
            //string strSql = null;
            //strSql = "SELECT TD001,TD002,TD003,RTRIM(TD004) TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,PURTC.TC003 AS TC003,PURTD.UDF08 AS PCH,PURMA.MA002 AS MA002,PURTD.UDF58 AS YTSL,TD021,COPMA.MA002 AS KHJC,TD013,TD023 ";
            //strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=PURTC.TC001 AND TD002=PURTC.TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON PURTC.TC004=PURMA.MA001 LEFT JOIN COPTC ON TD021=COPTC.TC002 LEFT JOIN COPMA ON COPTC.TC004=COPMA.MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
            //strSql += "AND TD018='Y' AND (PURTC.TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%') AND PURMA.MA002 LIKE '%" + txtGYS.Text.Trim() + "%' ORDER BY TD012";

            //try
            //{
            //    dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}

        }

        public void Update(string s)
        {

            this.txtTH.Text = s;

        }

        public void button2_Click(object sender, EventArgs e)
        {

            string strSql = null;

            strSql = "SELECT A.TA001 工单单别,A.TA002 工单单号,A.TA006 品号,A.TA034 品名,A.TA035 规格,B.TA003 工序号,B.TA024 工艺说明,B.TA006 供应商,MA002 供应商名称  FROM MOCTA A LEFT JOIN SFCTA B ON A.TA001=B.TA001 AND A.TA002=B.TA002 LEFT JOIN COPMA ON B.TA006=MA001 WHERE B.TA006='00'";

            try
            {
                dataGridView6.DataSource = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }


        }

        private void txtGYS_TextChanged(object sender, EventArgs e)
        {
            //string strSql = null;

            //strSql = "SELECT TD001,TD002,TD003,RTRIM(TD004) TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,PURTC.TC003 AS TC003,PURTD.UDF08 AS PCH,PURMA.MA002 AS MA002,PURTD.UDF58 AS YTSL,TD021,COPMA.MA002 AS KHJC,TD013,TD023 ";
            //strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=PURTC.TC001 AND TD002=PURTC.TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON PURTC.TC004=PURMA.MA001 LEFT JOIN COPTC ON TD021=COPTC.TC002 LEFT JOIN COPMA ON COPTC.TC004=COPMA.MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
            //strSql += "AND TD018='Y' AND (PURTC.TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%') AND PURMA.MA002 LIKE '%" + txtGYS.Text.Trim() + "%' ORDER BY TD012";

            //try
            //{
            //    dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }



        private void AppPO_Load(object sender, EventArgs e)
        {
            tabPage4.Parent = null;
            tabPage5.Parent = null;
            tabPage6.Parent = null;
            tabPage7.Parent = null;
            //string strSql = "";
            //strSql = "SELECT '" + PropertyClass.OperatorCode.Trim() + "'+CONVERT(CHAR(8),GETDATE(),112)+ISNULL(RIGHT('00'+RTRIM(CAST(CAST(MAX(RIGHT(RTRIM(PURTO.UDF04),3)) AS NUMERIC(11))+1 AS CHAR(11))),3) ,'001') AS 单号 FROM PURTO WHERE PURTO.UDF04 LIKE '" + PropertyClass.OperatorCode.Trim() + "' + CONVERT(CHAR(8),GETDATE(),112)+'%'";
            //        try
            //        {
            //            sdr1 = db.GetDataReader1(strSql);

            //            while (sdr1.Read())
            //            {
            //                textBox2.Text = sdr1["单号"].ToString();
            //            }

            //            if (sdr1.HasRows == false)
            //            {

            //            }

            //        }

            //        catch (Exception ex)
            //        {
            //            MessageBox.Show(ex.Message, "软件提示");
            //            throw ex;
            //        }
            //        finally
            //        {
            //            sdr1.Close();
            //        }



            //button4.PerformClick();
            //DataGridViewCheckBoxColumn colCA1 = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell a1bHeader = new DatagridViewCheckBoxHeaderCell();
            //colCA1.HeaderCell = a1bHeader;
            //colCA1.Name = "colCA1";
            //colCA1.HeaderText = "";
            //dataGridView4.Columns.Insert(0, colCA1);
            //a1bHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(abHeader_OnCheckBoxClicked);

            //DataGridViewCheckBoxColumn colCB1 = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell b1bHeader = new DatagridViewCheckBoxHeaderCell();
            //colCB1.HeaderCell = b1bHeader;
            //colCB1.Name = "colCB1";
            //colCB1.HeaderText = "";
            //colCB1.Width = 30;
            //rowMergeView1.Columns.Insert(0, colCB1);
            //b1bHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(bbHeader_OnCheckBoxClicked);

            //DataGridViewCheckBoxColumn colCC1 = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell c1bHeader = new DatagridViewCheckBoxHeaderCell();
            //colCC1.HeaderCell = c1bHeader;
            //colCC1.Name = "colCC1";
            //colCC1.HeaderText = "";
            //colCC1.Width = 30;
            //dataGridView6.Columns.Insert(0, colCC1);
            //c1bHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);

            //DataGridViewCheckBoxColumn colCD1 = new DataGridViewCheckBoxColumn();
            //DatagridViewCheckBoxHeaderCell d1bHeader = new DatagridViewCheckBoxHeaderCell();
            //colCD1.HeaderCell = d1bHeader;
            //colCD1.Name = "colCD1";
            //colCD1.Width = 30;
            //colCD1.HeaderText = "";
            //dataGridView1.Columns.Insert(0, colCD1);
            //d1bHeader.OnCheckBoxClicked +=
            //    new CheckBoxClickedHandler(cdHeader_OnCheckBoxClicked);

            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\采购单\");
            FileInfo[] fileInfo = theFolder.GetFiles();
            foreach (FileInfo NextFile in fileInfo)  //遍历文件
            {
                string gsmc = "";
                //gsmc = System.IO.Path.GetFileNameWithoutExtension(theFolder + NextFile.Name);
                gsmc = System.IO.Path.GetFileNameWithoutExtension(theFolder + NextFile.Name);
                comboBox2.Items.Add(gsmc);

            }



        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //string strSql = null;

            //strSql = "SELECT TD001,TD002,TD003,RTRIM(TD004) TD004,TD005,TD006,MB029,TD008,TD015,PURTD.UDF54 AS YZXS,PURTD.UDF56 AS DYSL,(TD008-TD015-PURTD.UDF56) AS DJSL,TD012,TD014,PURTD.UDF09 AS TZXX,PURTC.TC003 AS TC003,PURTD.UDF08 AS PCH,PURMA.MA002 AS MA002,PURTD.UDF58 AS YTSL,TD021,COPMA.MA002 AS KHJC,TD013,TD023 ";
            //strSql += "FROM PURTD LEFT JOIN PURTC ON TD001=PURTC.TC001 AND TD002=PURTC.TC002 LEFT JOIN INVMB ON TD004=MB001 LEFT JOIN PURMA ON PURTC.TC004=PURMA.MA001 LEFT JOIN COPTC ON TD021=COPTC.TC002 LEFT JOIN COPMA ON COPTC.TC004=COPMA.MA001 WHERE TD016<>'Y' AND TD016<>'y' ";
            //strSql += "AND TD018='Y' AND (PURTC.TC004='" + PropertyClass.OperatorCode + "' OR '" + PropertyClass.jiaose + "' LIKE '%采购管理%') AND (TD004 LIKE '%" + txtTH.Text.Trim() + "%' OR MB029 LIKE '%" + txtTH.Text.Trim() + "%') AND PURMA.MA002 LIKE '%" + txtGYS.Text.Trim() + "%' AND TD021 LIKE '%" + textBox1.Text.Trim() + "%' ORDER BY TD012";

            //try
            //{
            //    dataGridView1.DataSource = db.GetDataSet(strSql, "PURTD").Tables["PURTD"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //string strSql = null;

            //strSql = "SELECT A.TB032 是否急件,PURTA.TA003 下单日期,(CASE WHEN RTRIM(ISNULL(A.TB060,''))='' THEN RTRIM(PURTA.TA025) ELSE RTRIM(ISNULL(A.TB060,'')) END) 指令号,RTRIM(A.TB004) 品号,MB032 主供应商,RTRIM(ISNULL(PURMA.MA002,'')) 供应商名称,A.TB005 品名,A.TB006 规格,INVMB.MB110 型号,RTRIM(INVMB.UDF02) 材质,CEILING(A.TB009) 请购数量,A.TB007 请购单位,INVMB.MB028 备注,(CASE WHEN ISNULL(A.UDF01,'待询价')='' THEN '待询价' ELSE ISNULL(A.UDF01,'待询价') END) 工单进度,RTRIM(INVMB.UDF01) 型号,RTRIM(B.UDF09)+A.TB012 备注2,ISNULL(TD002,'') 采购单号,CEILING(MB036) 采购周期,(SELECT (CASE WHEN DATEADD(DAY,0,UDF06)>GETDATE()+3 THEN CONVERT(CHAR(8),DATEADD(DAY,0,UDF06),112) ELSE CONVERT(CHAR(8),GETDATE()+3,112) END) FROM CMSNO WHERE NO001=(CASE WHEN RTRIM(ISNULL(A.TB060,''))='' THEN RTRIM(PURTA.TA025) ELSE RTRIM(ISNULL(A.TB060,'')) END)) 计划完成日期,MIN(ISNULL(B.TB039,'')) 要求完工日期,TD012 最终确认交期,'' 供应商简称,'' 释放时间,'' 入库时间,TD008 应到货数量,TD015 实际到货数量,MB005 分类编号,ISNULL(INVMA.MA003,'') 分类名称,A.TB001 单别,A.TB002 单号,A.TB003 序号,ISNULL(DATEDIFF(DAY,PURTA.TA003,GETDATE()),0) 已下单天数 FROM PURTB A LEFT JOIN PURTA ON TB001=PURTA.TA001 AND TB002=PURTA.TA002 LEFT JOIN INVMB ON A.TB004=MB001 LEFT JOIN PURTR ON A.TB001=TR001 AND A.TB002=TR002 AND A.TB003=TR003 LEFT JOIN PURMA ON PURMA.MA001=MB032 LEFT JOIN INVMA ON MB005=INVMA.MA002 AND INVMA.MA001='1' LEFT JOIN MOCTB B ON B.TB003=A.TB004 AND B.TB038=A.TB060 LEFT JOIN PURTD ON TD026=A.TB001 AND TD027=A.TB002 AND TD028=A.TB003 LEFT JOIN MOCTA ON MOCTA.TA001=B.TB001 AND MOCTA.TA002=B.TB002 WHERE A.TB039='N' AND A.TB025='Y' AND (PURTA.TA025 LIKE '%" + textBox3.Text + "%' OR '" + textBox3.Text + "'='') AND ISNULL(A.UDF01,'')='" + comboBox1.Text + "' AND (MA047='" + PropertyClass.ygbh + "' OR '" + PropertyClass.jiaose + "'='计划员' OR '" + PropertyClass.jiaose + "'='采购经理') AND (MB032 = '" + textBox2.Text + "' OR PURMA.MA002 LIKE '%" + textBox2.Text + "%' OR '" + textBox2.Text + "'='') GROUP BY A.TB032,PURTA.TA003,(CASE WHEN RTRIM(ISNULL(A.TB060,''))='' THEN RTRIM(PURTA.TA025) ELSE RTRIM(ISNULL(A.TB060,'')) END),RTRIM(A.TB004),MB032,RTRIM(ISNULL(PURMA.MA002,'')),A.TB005,A.TB006,INVMB.MB110,RTRIM(INVMB.UDF02),CEILING(A.TB009),A.TB007,INVMB.MB028,(CASE WHEN ISNULL(A.UDF01,'待询价')='' THEN '待询价' ELSE ISNULL(A.UDF01,'待询价') END),RTRIM(INVMB.UDF01),A.TB012,ISNULL(TD002,''),CEILING(MB036),PURTD.TD012,PURTD.TD008,PURTD.TD015,INVMB.MB005,INVMA.MA003,A.TB001,A.TB002,A.TB003";

            //try
            //{
            //    db = new DataBase();
            //    dataGridView4.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //    //this.dataGridView4.Columns["图片"].Visible = false;
            //}
            //catch
            //{
               
            //}
        }

        private void button5_Click(object sender, EventArgs e)
        {

            this.tabControl2.SelectTab("tabPage5");


        }

        private void 查看图纸ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("iexplore.exe", dataGridView4.CurrentRow.Cells["图片"].Value.ToString());
        }


        private void button7_Click(object sender, EventArgs e)
        {
           // if(textBox7.Text=="")
           // {
           //     MessageBox.Show("供应商不可为空白","系统提示");
           //     return;
           // }
           // if (textBox14.Text == "")
           // {
           //     MessageBox.Show("项目号不可为空白", "系统提示");
           //     return;
           // }
           // string strSql = "";
           // string dh = null;
           // strSql = "SELECT ISNULL(CAST(CAST(MAX(TC002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM PURTC WHERE TC002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%' AND TC001 ='3310'";
           // try
           // {
           //     sdr1 = db.GetDataReader1(strSql);

           //     while (sdr1.Read())
           //     {
           //         dh = sdr1["单号"].ToString();

           //     }
           // }
           // catch
           // {
           // }
           // finally
           // {
           //     sdr1.Close();
           // }

           // string xmzlh =textBox14.Text;
           // string bz1 = textBox7.Text;
           // strSql = "INSERT INTO PURTC([COMPANY],[CREATOR],[USR_GROUP],[CREATE_DATE],[MODIFIER],[MODI_DATE],[FLAG],[TC001],[TC002],[TC003],[TC004],[TC005],[TC006],[TC007],[TC008],[TC009],[TC010],[TC011],[TC012],[TC013],[TC014],[TC015],[TC016],[TC017],[TC018],[TC019],[TC020],[TC021],[TC022],[TC023],[TC024],[TC025],[TC026],[TC027],[TC028],[TC029],[TC030],[TC031],[TC032],[TC033],[TC034],[TC035],[TC036],[TC037],[TC038],[TC039],[TC040],[TC041],[TC042],[TC043],[TC044],[TC045],[TC046],[TCD01],[TC047],[UDF01],[UDF02],[UDF03],[UDF04],[UDF05],[UDF06],[UDF51],[UDF52],[UDF53],[UDF54],[UDF55],[UDF56],[UDF07],[UDF08],[UDF09],[UDF10],[UDF11],[UDF12],[UDF57],[UDF58],[UDF59],[UDF60],[UDF61],[UDF62])SELECT 'CXA','" + PropertyClass.OperatorCode + "','" + PropertyClass.Partment + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'','',1,'3310','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),MA001,MA021,1,MA026,NA003,'" + textBox13.Text + "','01',MA047,'',0,'N','','','',MA044,0,0,'','',0,CONVERT(CHAR(8),GETDATE(),112),'" + PropertyClass.OperatorCode + "',MA064,MA055,0,0,'N',0,'','N','','N','','',0,0,0,'','','0',0,0,'','N','" + xmzlh + "','','','','" + textBox7.Text + "','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 FROM PURMA LEFT JOIN CMSNA ON NA001='1' AND NA002=MA027 WHERE MA001='" + textBox7.Text + "'";
           // try
           // {
           //     db = new DataBase();
           //     if (db.ExecDataBySql1(strSql) > 0)
           //     {
           //         foreach (DataGridViewRow dr in rowMergeView1.Rows)
           //         {
           //             if (Convert.ToBoolean(dr.Cells["colCB1"].Value) && dr.Cells["TB060"].Value.ToString().Trim()==xmzlh.Trim())
           //             {
           //                 strSql = "INSERT INTO PURTD([COMPANY],[CREATOR],[USR_GROUP],[CREATE_DATE],[MODIFIER],[MODI_DATE],[FLAG],[TD001],[TD002],[TD003],[TD004],[TD005],[TD006],[TD007],[TD008],[TD009],[TD010],[TD011],[TD012],[TD013],[TD014],[TD015],[TD016],[TD017],[TD018],[TD019],[TD020],[TD021],[TD022],[TD023],[TD024],[TD025],[TD026],[TD027],[TD028],[TD029],[TD030],[TD031],[TD032],[TD033],[TD034],[TD035],[TD036],[TD037],[TD038],[TD039],[TD040],[TD041],[TD042],[TD043],[TD044],[TD045],[TD046],[TD047],[TD048],[TD049],[TDC01],[TDC02],[TDC03],[TDC04],[TDC05],[TDC06],[TD050],[TD051],[UDF01],[UDF02],[UDF03],[UDF04],[UDF05],[UDF06],[UDF51],[UDF52],[UDF53],[UDF54],[UDF55],[UDF56],[UDF07],[UDF08],[UDF09],[UDF10],[UDF11],[UDF12],[UDF57],[UDF58],[UDF59],[UDF60],[UDF61],[UDF62])SELECT A.[COMPANY],A.[CREATOR],A.[USR_GROUP],A.[CREATE_DATE],A.[MODIFIER],A.[MODI_DATE],A.[FLAG],'3310','" + dh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TD003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTD WHERE TD001='3310' AND TD002='" + dh + "'),";
           //                 strSql += "'" + dr.Cells["TB004"].Value + "','" + dr.Cells["TB005"].Value + "','" + dr.Cells["TB006"].Value + "',(CASE WHEN TB060 LIKE '%C%' THEN MB017 ELSE '08' END)," + dr.Cells["TB009"].Value + ",MB004," + dr.Cells["ZBDJ"].Value + "," + dr.Cells["ZBDJ"].Value + " * " + dr.Cells["TB009"].Value + ",CONVERT(CHAR(8),GETDATE()+(CASE WHEN '" + textBox7.Text + "'=MB032 THEN INVMB.UDF55 WHEN '" + textBox7.Text + "'=INVMB.UDF06 THEN INVMB.UDF51 WHEN '" + textBox7.Text + "'=INVMB.UDF12 THEN INVMB.UDF53 ELSE 0 END),112),TB029,'" + dr.Cells["BZ1"].Value + "',0,'N','','N',0,'',TB030,TB060,TB031,'','N',TB001,TB002,TB003,'',0,0,'',MB111," + dr.Cells["ZBDJ"].Value + " * " + dr.Cells["TB009"].Value + ",0,0,0,MB004," + dr.Cells["TB009"].Value + ",'','','','','',0,0,0,0,0,0,0,'2',0,0,0,'','',(CASE WHEN '" + dr.Cells["TB019"].Value.ToString().Trim() + "'='' THEN A.TB011 ELSE '" + dr.Cells["TB019"].Value.ToString().Trim() + "' END),'','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 FROM PURTB A LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 WHERE A.TB001='" + dr.Cells["TB001"].Value + "' AND  A.TB002='" + dr.Cells["TB002"].Value + "' AND A.TB003='" + dr.Cells["TB003"].Value + "' AND TB039='N' AND TB025='Y' AND TA007='Y'";

           //                 try
           //                 {
           //                     db = new DataBase();
           //                     if (db.ExecDataBySql1(strSql) > 0)
           //                     {

           //                     }
           //                     else
           //                     {

           //                     }
           //                 }
           //                 catch
           //                 {

           //                 }
           //                 strSql = "UPDATE PURTR SET TR005='" + textBox7.Text + "' WHERE TR001='" + dr.Cells["TB001"].Value + "' AND TR002='" + dr.Cells["TB002"].Value + "' AND TR003='" + dr.Cells["TB003"].Value + "'";

           //                 try
           //                 {
           //                     if (db.ExecDataBySql1(strSql) > 0)
           //                     {
           //                     }
           //                     else
           //                     {
           //                     }
           //                 }
           //                 catch (Exception ex)
           //                 {
           //                     MessageBox.Show(ex.Message, "软件提示");
           //                     throw ex;
           //                 }

           //                 strSql = "UPDATE PURTB SET TB039='Y' WHERE TB001='" + dr.Cells["TB001"].Value + "' AND TB002='" + dr.Cells["TB002"].Value + "' AND TB003='" + dr.Cells["TB003"].Value + "'";

           //                 try
           //                 {
           //                     if (db.ExecDataBySql1(strSql) > 0)
           //                     {

           //                     }
           //                     else
           //                     {

           //                     }
           //                 }
           //                 catch
           //                 {

           //                 }
           //             }

           //         }

           //     }
           //     else
           //     {
           //         MessageBox.Show("现在忙,请稍后重试!", "系统提示");
              
           //     }
           // }
           // catch (Exception ex)
           // {
           //     MessageBox.Show(ex.Message, "软件提示");
           //     throw ex;
           // }



           //strSql = "UPDATE PURTC SET TC019=A.TD011,TC020=A.TD035,TC023=A.TD008 FROM (SELECT TD001,TD002,SUM(TD011) TD011,SUM(TD035) TD035,SUM(TD008) TD008  FROM PURTD GROUP BY TD001,TD002) A  WHERE TC001=A.TD001 AND TC002=A.TD002 AND TC001='3310' AND TC002='" + dh + "'";

           //                     try
           //                     {
           //                         if (db.ExecDataBySql1(strSql) > 0)
           //                         {
           //                         }
           //                         else
           //                         {

           //                         }
           //                     }
           //                     catch
           //                     {
           //                     }

           // strSql = "DELETE FROM PURTC WHERE NOT EXISTS(SELECT * FROM PURTD WHERE TD001=TC001 AND TD002=TC002) TC001='3310' AND TC002='" + dh + "'";

           // try
           // {
           //     if (db.ExecDataBySql1(strSql) > 0)
           //     {
           //         MessageBox.Show("没有任何符合条件的数据!", "软件提示");
           //         return;
           //     }
           //     else
           //     {

           //     }
           // }
           // catch
           // {
           // }

           // MessageBox.Show("采购单生成成功!", "软件提示");
           // if (checkBox2.CheckState == CheckState.Checked)
           //         {
           //           if(comboBox3.Text=="")
           //         {
           //             MessageBox.Show("没有对应的打印模板!","系统提示");
           //             return;
           //         }
                     

           //                     string x1 = "3310";
           //                     string x2 = dh;
           //                     string x3 = "";
           //                     string x4 = comboBox3.Text;
           //                     string x5 = "";
           //                     string x6 = "";
           //                     string x7 = "";
           //                     string x8 = "";
           //                     JYCX xx = new JYCX(x1, x2, x3, x4, x5, x6, x7, x8);
           //                     POPrint L = new POPrint();
           //                     L.cdssh = new POPrint.Cdssh(textBox2_TextChanged);
           //                     L.Show();
           //           }     
            

           // button6.PerformClick();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string fileName = "123";

            string saveFileName = "";

            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.DefaultExt = "xls";

            saveDialog.Filter = "Excel文件|*.xls";

            saveDialog.FileName = fileName;

            saveDialog.ShowDialog();

            saveFileName = saveDialog.FileName;

            if (saveFileName.IndexOf(":") < 0) return; //被点了取消

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {

                MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");

                return;

            }

            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 

            //写入标题             

            for (int i = 0; i < rowMergeView1.ColumnCount; i++)

            { worksheet.Cells[1, i + 1] = rowMergeView1.Columns[i].HeaderText; }

            //写入数值

            for (int r = 0; r < rowMergeView1.Rows.Count; r++)
            {
                for (int i = 0; i < rowMergeView1.ColumnCount; i++)
                {

                    worksheet.Cells[r + 2, i + 1] = rowMergeView1.Rows[r].Cells[i].Value;

                }

                System.Windows.Forms.Application.DoEvents();

            }

            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);

            if (saveFileName != "")
            {

                try
                {
                    workbook.Saved = true;

                    workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 

                }

                catch (Exception ex)
                {//fileSaved = false;                      

                    MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);

                }

            }

            xlApp.Quit();

            GC.Collect();//强行销毁       
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }


        private void dataGridView4_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            //f = new GYSKC();
            //f.DataSelected += new EventHandler(j_DataSelected);
            //f.cdgzzx += new GYSKC.Cdgzzx(textBox2_TextChanged);
            //f.Show(this);
        }

        void j_DataSelected(object sender, EventArgs e)
        {

            //textBox4.Text = f.UserId;
            //label8.Text = f.UserName;

        }

        private void button10_Click(object sender, EventArgs e)
        {
            string strSql = "";
            foreach (DataGridViewRow dr in dataGridView6.Rows)
            {

                if (dr.Index < dataGridView6.Rows.Count - 1)
                {
                    if (Convert.ToBoolean(dr.Cells["colCC1"].Value))
                    {

                        if (textBox4.Text != "")
                        {
                            strSql = "UPDATE SFCTA SET TA006='" + textBox4.Text + "',TA007='" + label8.Text + "' WHERE TA001='" + dr.Cells["工单单别"].Value + "' AND TA002='" + dr.Cells["工单单号"].Value + "' AND TA003='" + dr.Cells["工序号"].Value + "'";

                            try
                            {
                                db = new DataBase();
                                if (db.ExecDataBySql1(strSql) > 0)
                                {

                                }
                                else
                                {
                                    MessageBox.Show("更新失败，请检查", "软件提示");
                                    return;
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }
                        }
                    }
                }
            }
        }

        private void dataGridView4_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //System.Data.DataTable dt;
            //this.tabControl2.SelectTab("tabPage5");
            //string strSql = null;
            //int k = 0;
            //string egys = "";
            //string sgys = "";
            //string zgys = (dataGridView4.CurrentRow.Cells["主供应商"].Value == null) ? "" : dataGridView4.CurrentRow.Cells["主供应商"].Value.ToString();
            //if (zgys != "")
            //{
            //    this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1].Tag = dataGridView4.CurrentRow.Cells["主供应商"].Value.ToString();
            //    this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1].ToolTipText = dataGridView4.CurrentRow.Cells["供应商名称"].Value.ToString();
            //    this.rowMergeView1.AddSpanHeader(rowMergeView1.Columns["TB019"].Index + 1, 5, dataGridView4.CurrentRow.Cells["供应商名称"].Value.ToString());
            //    k = k + 1;
            //}

            //strSql = "SELECT RTRIM(INVMB.UDF06) GYS,MA002 FROM INVMB LEFT JOIN PURMA ON RTRIM(INVMB.UDF06)=MA001 WHERE MB001='" + dataGridView4.CurrentRow.Cells["品号"].Value.ToString() + "' AND ISNULL(INVMB.UDF06,'')<>''";
            //try
            //{
            //    sdr1 = db.GetDataReader1(strSql);
            //    while (sdr1.Read())
            //    {
            //        this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1 + 5 * k].Tag = sdr1["GYS"].ToString();
            //        this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1 + 5 * k].ToolTipText = sdr1["MA002"].ToString(); ;
            //        this.rowMergeView1.AddSpanHeader(rowMergeView1.Columns["TB019"].Index + 1 + 5 * k, 5, sdr1["MA002"].ToString());
            //        egys = sdr1["GYS"].ToString();
            //        k = k + 1;
            //    }
            //}

            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
            //finally
            //{
            //    sdr1.Close();
            //}

            //strSql = "SELECT RTRIM(INVMB.UDF12) GYS,MA002 FROM INVMB LEFT JOIN PURMA ON RTRIM(INVMB.UDF12)=MA001 WHERE MB001='" + dataGridView4.CurrentRow.Cells["品号"].Value.ToString() + "'AND ISNULL(INVMB.UDF12,'')<>''";
            //try
            //{
            //    sdr1 = db.GetDataReader1(strSql);

            //    while (sdr1.Read())
            //    {
            //        this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1 + 5 * k].Tag = sdr1["GYS"].ToString();
            //        this.rowMergeView1.Columns[rowMergeView1.Columns["TB019"].Index + 1 + 5 * k].ToolTipText = sdr1["MA002"].ToString(); ;
            //        this.rowMergeView1.AddSpanHeader(rowMergeView1.Columns["TB019"].Index + 1 + 5 * k, 5, sdr1["MA002"].ToString());
            //        sgys = sdr1["GYS"].ToString();
            //        k = k + 1;
            //    }
            //}

            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
            //finally
            //{
            //    sdr1.Close();
            //}

            ////strSql = "SELECT DISTINCT RTRIM(MB032) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(MB032)=MA001 WHERE ((INVMB.UDF06='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF06='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF06='" + sgys + "' AND '" + sgys + "'<>'')) AND MB032<>'" + zgys + "' AND MB032<>'" + egys + "' AND MB032<>'" + sgys + "' AND MB032<>''";
            ////strSql += " UNION ALL SELECT RTRIM(MB032) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(MB032)=MA001 WHERE ((INVMB.UDF12='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF12='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF12='" + sgys + "' AND '" + sgys + "'<>'') AND MB032<>'" + zgys + "' AND MB032<>'" + egys + "' AND MB032<>'" + sgys + "' AND MB032<>''";
            //strSql += "SELECT RTRIM(INVMB.UDF06) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF06)=MA001 WHERE ((MB032='" + zgys + "' AND '" + zgys + "'<>'') AND INVMB.UDF06<>'" + zgys + "' AND INVMB.UDF06<>'" + egys + "' AND INVMB.UDF06<>'" + sgys + "' AND INVMB.UDF06<>''";
            ////strSql += " UNION ALL SELECT RTRIM(INVMB.UDF06) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF06)=MA001 WHERE ((INVMB.UDF12='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF12='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF12='" + sgys + "' AND '" + sgys + "'<>'')) AND INVMB.UDF06<>'" + zgys + "' AND INVMB.UDF06<>'" + egys + "' AND INVMB.UDF06<>'" + sgys + "' AND INVMB.UDF06<>''";
            //strSql += " UNION ALL SELECT RTRIM(INVMB.UDF12) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF12)=MA001 WHERE ((MB032='" + zgys + "' AND '" + zgys + "'<>'') AND INVMB.UDF12<>'" + zgys + "' AND INVMB.UDF12<>'" + egys + "' AND INVMB.UDF12<>'" + sgys + "' AND INVMB.UDF12<>''";
            ////strSql += " UNION ALL SELECT RTRIM(INVMB.UDF12) GYS,MA002 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF12)=MA001 WHERE ((INVMB.UDF06='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF06='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF06='" + sgys + "' AND '" + sgys + "'<>'')) AND INVMB.UDF12<>'" + zgys + "' AND INVMB.UDF12<>'" + egys + "' AND INVMB.UDF12<>'" + sgys + "' AND INVMB.UDF12<>''";

            //try
            //{
            //    db = new DataBase();
            //    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];

            //    for (int i = k; i <= 7; i++)
            //    {
            //        this.rowMergeView1.ColumnHeadersHeight = 40;
            //        this.rowMergeView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //        for (int j = rowMergeView1.Columns["TB019"].Index + 1 + i * 5; j < rowMergeView1.Columns["TB019"].Index + 1 + (i + 1) * 5; j++)
            //        {
            //            this.rowMergeView1.Columns[j].Tag = dt.Rows[i][0].ToString();
            //            this.rowMergeView1.Columns[j].ToolTipText = dt.Rows[i][1].ToString();
            //        }
            //        this.rowMergeView1.AddSpanHeader(rowMergeView1.Columns["TB019"].Index + 1 + i * 5, 5, dt.Rows[i][1].ToString());
            //    }
            //}
            //catch
            //{

            //}

            //strSql = "SELECT TB001 单别,TB002 单号,TB003 序号,TB060 项目号,RTRIM(TB004) 品号,TB005 品名,TB006 规格,PURTB.UDF02 材质,CEILING(TB009) 订单数量,PURTB.UDF01 工单进度,TB011 预计交货日 FROM PURTB LEFT JOIN INVMB ON TB004=MB001 WHERE TB039='N' AND ((INVMB.MB032='" + zgys + "' AND '" + zgys + "'<>'') OR (MB032='" + egys + "' AND '" + egys + "'<>'') OR (MB032='" + sgys + "' AND '" + sgys + "'<>'') OR (INVMB.UDF06='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF06='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF06='" + sgys + "' AND '" + sgys + "'<>'') OR (INVMB.UDF12='" + zgys + "' AND '" + zgys + "'<>'') OR (INVMB.UDF12='" + egys + "' AND '" + egys + "'<>'') OR (INVMB.UDF12='" + sgys + "' AND '" + sgys + "'<>''))";
            //try
            //{
            //    db = new DataBase();
            //    dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
            //    rowMergeView1.Rows.Clear();
            //    for (int i = 0; i <= dt.Rows.Count - 1; i++)
            //    {
            //        int index = this.rowMergeView1.Rows.Add();
            //        this.rowMergeView1.Rows[index].Cells[0].Value = false;
            //        this.rowMergeView1.Rows[index].Cells[1].Value = dt.Rows[i][0].ToString();
            //        this.rowMergeView1.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
            //        this.rowMergeView1.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
            //        this.rowMergeView1.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
            //        this.rowMergeView1.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
            //        this.rowMergeView1.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
            //        this.rowMergeView1.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
            //        this.rowMergeView1.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
            //        this.rowMergeView1.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
            //        this.rowMergeView1.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
            //        this.rowMergeView1.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();

            //    }
            //    this.rowMergeView1.Columns["TB019"].Frozen = true;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void dataGridView4_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void rowMergeView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void rowMergeView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void rowMergeView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button4.PerformClick();
        }

        private void rowMergeView1_SelectionChanged(object sender, EventArgs e)
        {
            string strSql = "";
            try
            {
                String qgdb = (rowMergeView1.CurrentRow.Cells["TB001"].Value == null) ? "" : rowMergeView1.CurrentRow.Cells["TB001"].Value.ToString();
                String qgdh = (rowMergeView1.CurrentRow.Cells["TB002"].Value == null) ? "" : rowMergeView1.CurrentRow.Cells["TB002"].Value.ToString();
                String qgxh = (rowMergeView1.CurrentRow.Cells["TB003"].Value == null) ? "" : rowMergeView1.CurrentRow.Cells["TB003"].Value.ToString();

                strSql = "SELECT TP004 供应商编号,TP005 品名,TP007 是否含税,TP010 AS 报价,TO004,TO005,TO006,TP001,TP002,TP003 FROM PURTO LEFT JOIN PURTP ON TP001=TO001 AND TP002=TO002 WHERE  PURTO.UDF01='" + qgdb + "' AND PURTO.UDF02='" + qgdh + "' AND PURTO.UDF03='" + qgxh + "'";
                System.Data.DataTable dt;
                dataGridView5.Rows.Clear();
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    int index = this.dataGridView5.Rows.Add();
                    this.dataGridView5.Rows[index].Cells[0].Value = dt.Rows[i][0].ToString();
                    this.dataGridView5.Rows[index].Cells[1].Value = dt.Rows[i][1].ToString();
                    this.dataGridView5.Rows[index].Cells[2].Value = dt.Rows[i][2].ToString();
                    this.dataGridView5.Rows[index].Cells[3].Value = dt.Rows[i][3].ToString();
                    this.dataGridView5.Rows[index].Cells[4].Value = dt.Rows[i][4].ToString();
                    this.dataGridView5.Rows[index].Cells[5].Value = dt.Rows[i][5].ToString();
                    this.dataGridView5.Rows[index].Cells[6].Value = dt.Rows[i][6].ToString();
                    this.dataGridView5.Rows[index].Cells[7].Value = dt.Rows[i][7].ToString();
                    this.dataGridView5.Rows[index].Cells[8].Value = dt.Rows[i][8].ToString();
                    this.dataGridView5.Rows[index].Cells[9].Value = dt.Rows[i][9].ToString();
                }

            }
            catch
            {

            }
        }



        private void button6_Click(object sender, EventArgs e)
        {
            //ExtensionMethods.DoubleBuffered(rowMergeView1, true);

            //string strSql = "";
            //System.Data.DataTable dt;
            //this.rowMergeView1.Rows.Clear();
            //rowMergeView1.ClearSpanInfo();


            //strSql = "SELECT DISTINCT TB001,TB002,TB003,TB060,RTRIM(TB004) TB004,TB005,TB006,MB028 BZ1,ISNULL(INVMB.UDF01,'') CPXH,ISNULL(INVMB.UDF02,'') CZ,CEILING(TB009) TB009,PURTB.UDF01 GDJD,ISNULL((SELECT (CASE WHEN DATEADD(DAY,0,UDF06)>GETDATE()+3 THEN CONVERT(CHAR(8),DATEADD(DAY,0,UDF06),112) ELSE CONVERT(CHAR(8),GETDATE()+3,112) END) FROM CMSNO WHERE NO001=(CASE WHEN RTRIM(ISNULL(TB060,''))='' THEN RTRIM(PURTA.TA025) ELSE RTRIM(ISNULL(TB060,'')) END)),'') AS TB019,RTRIM(TP004),RTRIM(MA002),CAST(TP010 AS NUMERIC(16,4)),ISNULL(TO001,''),ISNULL(TO002,''),ISNULL(TO003,'') FROM PURTB LEFT JOIN PURTO ON RTRIM(PURTO.UDF01)=RTRIM(TB001) AND RTRIM(PURTO.UDF02)=RTRIM(TB002) AND RTRIM(PURTO.UDF03)=RTRIM(TB003) LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN PURTP ON TO001=TP001 AND TO002=TP002 AND TP015='Y' LEFT JOIN INVMB ON TB004=MB001 LEFT JOIN PURMA ON ISNULL(TP004,MB032)=MA001 WHERE ISNULL(TO001,'')<>'' AND TB025='Y' AND TA007='Y' AND TB039='N' AND (MA047='" + PropertyClass.ygbh + "' OR '" + PropertyClass.jiaose + "'='计划员') AND (TP004 = '" + textBox7.Text + "' OR PURMA.MA002 LIKE '%" + textBox7.Text + "%' OR '" + textBox7.Text + "'='') AND (TB060 LIKE '%" + textBox14.Text + "%' OR '" + textBox14.Text + "'='')";

            //try
            //{
            //    db = new DataBase();
            //    dt = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //    //var oldAutoSizeRowsMode = this.rowMergeView1.AutoSizeRowsMode;
            //    //var oldAutoSizeColumnsMode = this.rowMergeView1.AutoSizeColumnsMode;
            //    //this.rowMergeView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
            //    //this.rowMergeView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;


            //    rowMergeView1.Rows.Clear();
            //    for (int i = 0; i <= dt.Rows.Count - 1; i++)
            //    {
            //        int index = this.rowMergeView1.Rows.Add();
            //        this.rowMergeView1.Rows[index].Cells[0].Value = false;
            //        this.rowMergeView1.Rows[index].Cells[1].Value = dt.Rows[i][0].ToString();
            //        this.rowMergeView1.Rows[index].Cells[2].Value = dt.Rows[i][1].ToString();
            //        this.rowMergeView1.Rows[index].Cells[3].Value = dt.Rows[i][2].ToString();
            //        this.rowMergeView1.Rows[index].Cells[4].Value = dt.Rows[i][3].ToString();
            //        this.rowMergeView1.Rows[index].Cells[5].Value = dt.Rows[i][4].ToString();
            //        this.rowMergeView1.Rows[index].Cells[6].Value = dt.Rows[i][5].ToString();
            //        this.rowMergeView1.Rows[index].Cells[7].Value = dt.Rows[i][6].ToString();
            //        this.rowMergeView1.Rows[index].Cells[8].Value = dt.Rows[i][7].ToString();
            //        this.rowMergeView1.Rows[index].Cells[9].Value = dt.Rows[i][8].ToString();
            //        this.rowMergeView1.Rows[index].Cells[10].Value = dt.Rows[i][9].ToString();
            //        this.rowMergeView1.Rows[index].Cells[11].Value = dt.Rows[i][10].ToString();
            //        this.rowMergeView1.Rows[index].Cells[12].Value = dt.Rows[i][11].ToString();
            //        this.rowMergeView1.Rows[index].Cells[13].Value = dt.Rows[i][12].ToString();
            //        this.rowMergeView1.Rows[index].Cells[14].Value = dt.Rows[i][13].ToString();
            //        this.rowMergeView1.Rows[index].Cells[15].Value = dt.Rows[i][14].ToString();
            //        this.rowMergeView1.Rows[index].Cells[16].Value = dt.Rows[i][15].ToString();
            //        this.rowMergeView1.Rows[index].Cells[17].Value = dt.Rows[i][16].ToString();
            //        this.rowMergeView1.Rows[index].Cells[18].Value = dt.Rows[i][17].ToString();
            //        this.rowMergeView1.Rows[index].Cells[19].Value = dt.Rows[i][18].ToString();

            //    }

            //    //this.rowMergeView1.AutoSizeRowsMode = oldAutoSizeRowsMode;
            //    //this.rowMergeView1.AutoSizeColumnsMode = oldAutoSizeColumnsMode;
            //}
            //catch
            //{

            //}

        }
    

        private void button11_Click(object sender, EventArgs e)
        {
        //    try
        //    {
        //        OpenFileDialog dialog = new OpenFileDialog();
        //        dialog.Multiselect = true;//该值确定是否可以选择多个文件
        //        dialog.Title = "请选择文件夹";
        //        dialog.Filter = "所有文件(*.*)|*.*";
        //        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //        {
        //            string file = dialog.FileName;
        //            string fileex = System.IO.Path.GetFileName(file);
        //            //System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(GetImagePath());
        //            //int wjgs = GetFilesCount(dirInfo) + 1;
        //            //string wjmc = wjgs.ToString();
        //            //wjmc = "00" + wjmc;
        //            //wjmc = wjmc.Substring(wjmc.Length - 3, 3);
        //            //string picName = GetImagePath() + "\\" + wjmc + fileex;
        //            //string picName = GetImagePath() + "\\" + fileex;
        //            //if (File.Exists(picName))
        //            //{
        //            //    File.Delete(picName);
        //            //    //picName = GetNewFilname();
        //            //}
        //            WebClient myWebClient = new WebClient();
        //            byte[] responseArray = myWebClient.UploadFile("http://192.168.0.25:8080/MyFirstWebAPP/DuploadServlet", "POST", file);
        //            string ass = System.Text.Encoding.UTF8.GetString(responseArray);
        //            Root rt = JsonConvert.DeserializeObject<Root>(ass);
                   
        //        string strSql = "";
        //        strSql = "UPDATE PURTP SET UDF07='" + rt.@params.Result.ToString() + "' WHERE TP001='" + dataGridView5.CurrentRow.Cells["BTP001"].Value.ToString() + "' AND TP002='" + dataGridView5.CurrentRow.Cells["BTP002"].Value.ToString() + "' AND TP003='" + dataGridView5.CurrentRow.Cells["BTP003"].Value.ToString() + "'";
        //        try
        //        {
        //            db = new DataBase();
        //            if (db.ExecDataBySql1(strSql) > 0)
        //            {
        //                MessageBox.Show("上传成功，请检查", "软件提示");
        //            }
        //            else
        //            {
        //                MessageBox.Show("上传失败，请检查", "软件提示");
        //                return;
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            MessageBox.Show(ex.Message, "软件提示");
        //            throw ex;
        //        }
        //    }
        //}
        //    catch
        //    {

        //    }
        //}
        
        //    public static int GetFilesCount(System.IO.DirectoryInfo dirInfo)
        //{
        //    int totalFile = 0;
        //    totalFile += dirInfo.GetFiles().Length;
        //    foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories())
        //    {
        //        totalFile += GetFilesCount(subdir);
        //    }
        //    return totalFile;
        }

        private string GetImagePath()
        {
            
            string personImgPath = ENCODE.DecryptDES(OperatorFile.GetIniFileString("DataBase", "imgPath", "",
                                                             System.Windows.Forms.Application.StartupPath + "\\SCM.ini"), "digiwinw")
                         + Path.DirectorySeparatorChar.ToString() + "SCMImg" + "\\" + dataGridView5.CurrentRow.Cells["TO004"].Value.ToString().Trim() + "\\" + dataGridView5.CurrentRow.Cells["BTP002"].Value.ToString().Trim() + dataGridView5.CurrentRow.Cells["BTP003"].Value.ToString().Trim() + "";
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }
            return personImgPath;

        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("询价批次号不可为空","系统提示");
                return;
            }
            string strSql = "";
            string dh = "";
            for (int i = 0; i <= this.dataGridView4.Rows.Count - 1; i++)
            {
                string ss = (this.dataGridView4.Rows[i].Cells["工单进度"].Value == null) ? "" : this.dataGridView4.Rows[i].Cells["工单进度"].Value.ToString();
                if (Convert.ToBoolean(this.dataGridView4.Rows[i].Cells["colCA1"].Value) && ss=="待询价")
                {
                    strSql = "SELECT ISNULL(CAST(CAST(MAX(TO002) AS NUMERIC(11))+1 AS CHAR(11)) ,CONVERT(CHAR(8),GETDATE(),112)+'001') AS 单号 FROM PURTO WHERE TO002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%' AND TO001 ='3610'";
                   
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            dh = sdr1["单号"].ToString();

                        }

                        if (sdr1.HasRows == false)
                        {

                        }

                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                    strSql = "INSERT INTO PURTO(COMPANY,CREATOR,CREATE_DATE,FLAG,TO001,TO002,TO003,TO004,TO005,TO006,TO007,TO008,TO009,TO010,TO011,TO012,TO013,TO014,TO015,TO016,TO017,TO018,TO019,UDF01,UDF02,UDF03,UDF04) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'3610','" + dh + "',CONVERT(CHAR(8),GETDATE(),112),MB001,MB002,MB003,'N','',0,CONVERT(CHAR(8),GETDATE(),112),'','N',0,'','','',0,0,0,'" + this.dataGridView4.Rows[i].Cells["单别"].Value + "','" + this.dataGridView4.Rows[i].Cells["单号"].Value + "','" + this.dataGridView4.Rows[i].Cells["序号"].Value + "','" + this.dataGridView4.Rows[i].Cells["供应商名称"].Value + "' FROM INVMB WHERE MB001='" + this.dataGridView4.Rows[i].Cells["品号"].Value + "'  AND NOT EXISTS(SELECT * FROM PURTO WHERE TO001='3610' AND TO002='" + dh + "')";
                 
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    strSql = "INSERT INTO PURTP(COMPANY,CREATOR,CREATE_DATE,FLAG,TP001,TP002,TP003,TP004,TP005,TP006,TP007,TP008,TP009,TP010,TP011,TP012,TP013,TP014,TP015,TP016,TP017,TP018,TP019,TP020,TP021,TP022,TP023) SELECT 'CXA','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'3610','" + dh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TP003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTP WHERE TP001='3610' AND TP002='" + dh + "'),MB032,MA002,'RMB','Y','','N',0,'N','',CONVERT(CHAR(8),GETDATE(),112),'','N',MB149,'','','','',0,0,0 FROM PURTO LEFT JOIN INVMB ON TO004=MB001 LEFT JOIN PURMA ON MB032=MA001 WHERE TO001='3610' AND TO002='" + dh + "' AND ISNULL(MB032,'')<>''";
                   
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                            strSql = "UPDATE PURTB SET UDF01='已询价' WHERE TB001='" + this.dataGridView4.Rows[i].Cells["单别"].Value + "' AND TB002='" + this.dataGridView4.Rows[i].Cells["单号"].Value + "' AND TB003='" + this.dataGridView4.Rows[i].Cells["序号"].Value + "'";
                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                    dataGridView4.Rows[i].Cells["工单进度"].Value = "已询价";
                                }
                                else
                                {

                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "软件提示");
                                throw ex;
                            }
                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    strSql = "INSERT INTO PURTP(COMPANY,CREATOR,CREATE_DATE,FLAG,TP001,TP002,TP003,TP004,TP005,TP006,TP007,TP008,TP009,TP010,TP011,TP012,TP013,TP014,TP015,TP016,TP017,TP018,TP019,TP020,TP021,TP022,TP023) SELECT 'CX','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'3610','" + dh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TP003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTP WHERE TP001='3610' AND TP002='" + dh + "'),RTRIM(INVMB.UDF06),MA002,'RMB','Y','','N',0,'N','',CONVERT(CHAR(8),GETDATE(),112),'','N',MB149,'','','','',0,0,0 FROM PURTO LEFT JOIN INVMB ON TO004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF06)=MA001 WHERE TO001='3610' AND TO002='" + dh + "' AND ISNULL(INVMB.UDF06,'')<>''";
             
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }

                    strSql = "INSERT INTO PURTP(COMPANY,CREATOR,CREATE_DATE,FLAG,TP001,TP002,TP003,TP004,TP005,TP006,TP007,TP008,TP009,TP010,TP011,TP012,TP013,TP014,TP015,TP016,TP017,TP018,TP019,TP020,TP021,TP022,TP023) SELECT 'CX','" + PropertyClass.OperatorCode + "',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),1,'3610','" + dh + "',(SELECT RIGHT(RTRIM('000'+ISNULL(CAST(CAST(MAX(TP003) AS NUMERIC(4))+1 AS CHAR(4)),'0001')),4) FROM PURTP WHERE TP001='3610' AND TP002='" + dh + "'),RTRIM(INVMB.UDF12),MA002,'RMB','Y','','N',0,'N','',CONVERT(CHAR(8),GETDATE(),112),'','N',MB149,'','','','',0,0,0 FROM PURTO LEFT JOIN INVMB ON TO004=MB001 LEFT JOIN PURMA ON RTRIM(INVMB.UDF12)=MA001 WHERE TO001='3610' AND TO002='" + dh + "' AND ISNULL(INVMB.UDF12,'')<>''";
              
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {

                        }
                        else
                        {

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        throw ex;
                    }
                }
            }
            MessageBox.Show("询价单生成成功！", "软件提示");
            button4.PerformClick();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //string strSql = null;
            //strSql = " SELECT TB060 指令号,TP004 供应商编号,MA002 供应商名称,TO001 询价单别,TO002 询价单号,TO003 询价日期,TP003 询价序号,TO004 品号,TO005 品名,TO006 规格,MB004 单位,INVMB.UDF01 型号,INVMB.UDF02 材质,CAST(TB009 AS REAL) 数量,CAST(TP010 AS REAL) 单价,TP007 是否含税,PURTO.UDF01 请购单别,PURTO.UDF02 请购单号,PURTO.UDF03 请购序号 FROM PURTO LEFT JOIN PURTP ON TO001=TP001 AND TO002=TP002 LEFT JOIN PURMA ON TP004=MA001 LEFT JOIN PURTB ON PURTO.UDF01=TB001 AND PURTO.UDF02=TB002 AND PURTO.UDF03=TB003 LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 LEFT JOIN INVMB ON TO004=MB001 LEFT JOIN CMSMV ON MA047=MV001 WHERE (MV002='" + textBox11.Text + "' OR MA047='" + textBox11.Text + "' OR '" + textBox11.Text + "'='') AND TB039='N' AND (MA002 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='')  AND ((ISNULL(TA025,'') LIKE '%" + textBox5.Text + "%' OR '" + textBox5.Text + "'=''))  AND (MA047='" + PropertyClass.ygbh + "' OR '" + PropertyClass.jiaose + "'='计划员')";
            //try
            //{
            //    db = new DataBase();
            //    dataGridView7.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void button15_Click(object sender, EventArgs e)
        {
                    string fileName = textBox6.Text;

                    string saveFileName = "";

                    SaveFileDialog saveDialog = new SaveFileDialog();

                    saveDialog.DefaultExt = "xls";

            saveDialog.Filter = "Excel 97-2003 工作薄|*.xls";

            saveDialog.FileName = fileName;

                    saveDialog.ShowDialog();

                    saveFileName = saveDialog.FileName;

                    if (saveFileName.IndexOf(":") < 0) return; //被点了取消

                    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                    if (xlApp == null)
                    {

                        MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");

                        return;

                    }

                    var workbooks = xlApp.Workbooks;

                    var workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

                    var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 

                    //写入标题             

                    for (int i = 0; i < dataGridView7.ColumnCount; i++)

                    { worksheet.Cells[1, i + 1] = dataGridView7.Columns[i].HeaderText; }

                    //写入数值

                    for (int r = 0; r < dataGridView7.Rows.Count; r++)
                    {
                        for (int i = 0; i < dataGridView7.ColumnCount; i++)
                        {

                            worksheet.Cells[r + 2, i + 1] = dataGridView7.Rows[r].Cells[i].Value;

                        }

                        System.Windows.Forms.Application.DoEvents();

                    }

                    worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

                    MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);

                    if (saveFileName != "")
                    {

                        try
                        {
                            workbook.Saved = true;

                      //      workbook.SaveCopyAs(saveFileName);  //fileSaved = true;
                                                                //workbook
                    workbook.SaveAs(saveFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8);

                }

                catch (Exception ex)
                        {//fileSaved = false;                      

                            MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);

                        }

                    }

                    xlApp.Quit();

                    GC.Collect();//强行销毁       
            
      }

        private void button16_Click(object sender, EventArgs e)
        {
            Application.DoEvents();
            this.dataGridView7.DataSource = null;
            bool fflag = true;
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = System.Environment.CurrentDirectory;
            open.Title = "请选择要导入的Excel文件";
            open.Filter = "Excel文件|*.xls";
            if (open.ShowDialog() == DialogResult.OK)
            {
                string fileName = open.FileName;
                //MessageBox.Show(fileName, "提示");
                //根据路径打开一个Excel文件并将数据填充到DataSet中
                //string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + Server.MapPath("ExcelFiles/MyExcelFile.xls") + ";Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件
               // string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + fileName + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)
              //  string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";


                //备注： "HDR=yes;"是说Excel文件的第一行是列名而不是数据，"HDR=No;"正好与前面的相反。
                //      "IMEX=1 "如果列中的数据类型不一致，使用"IMEX=1"可必免数据类型冲突。 

                //     OleDbConnection conn = new OleDbConnection(strConn);
                // OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
                //   DataSet ds = new DataSet();
                //    adp.Fill(ds, "Book1");
          
                DataSet vDsExcel;
                vDsExcel = ImportExcel(fileName);
          
                this.dataGridView7.DataSource = vDsExcel.Tables[0].DefaultView;
                //this.dataGridView1.DataBind();
            }
        }
        private DataSet ImportExcel(string pFileName)
        {
            FileInfo vFileInfo = new FileInfo(pFileName);
            if (!vFileInfo.Exists)
                return null;

            string vConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pFileName + ";Extended Properties='Excel 8.0;HDR=yes;IMEX=1'";
            OleDbConnection vObjConn = new OleDbConnection(vConn);
            DataSet vDSExcel = new DataSet();
            try
            {
                vObjConn.Open();
                string vSql = "select * from  [Sheet1$]";
                OleDbDataAdapter odbcExcelDataAdapter = new OleDbDataAdapter(vSql, vObjConn);
                odbcExcelDataAdapter.Fill(vDSExcel);
                return vDSExcel;
            }
            catch (Exception vEx)
            {
                MessageBox.Show("加载文件错误：" + vEx.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return vDSExcel;
            }
        }
        private void button17_Click(object sender, EventArgs e)
        {
            string strSql = "";
             foreach (DataGridViewRow dr in dataGridView7.Rows)
            {
                double cgdj = (dr.Cells["单价"].Value == null) ? 0.00 : Convert.ToDouble(dr.Cells["单价"].Value.ToString());
                strSql = "UPDATE PURTP SET TP010=" + cgdj + " WHERE TP001='" + dr.Cells["询价单别"].Value + "' AND TP002='" + dr.Cells["询价单号"].Value + "' AND TP003=RIGHT('000'+'" + dr.Cells["询价序号"].Value + "',4)";

                            try
                            {
                                if (db.ExecDataBySql1(strSql) > 0)
                                {
                                    
                                }
                                else
                                {
                                   
                                }
                            }
                            catch 
                            {
                              
                            }
                        }
            MessageBox.Show("更新成功！","系统提示");
        }

        private void button20_Click(object sender, EventArgs e)
        {
            string strSql;
            //strSql = "SELECT TC001 采购单别,TC002 采购单号,TC003 审核日期,TC004 供应商代码,MA002 供应商名称,TC027 付款条件编号,ISNULL(NA003,'') 付款条件,TC005 币种,CAST(TC006 AS REAL) 汇率,TC031 传送次数,'' 正文内容,MA011 收件人 FROM PURTC LEFT JOIN PURMA ON TC004=MA001 LEFT JOIN CMSNA ON TC027=NA002 WHERE TC031=0 AND TC014='Y'";
            strSql = @"SELECT DISTINCT PURCHASE_ORDER.DOC_NO 采购单号,CONVERT(CHAR(8),PURCHASE_ORDER.ApproveDate,112) 审核日期,SUPPLIER.SUPPLIER_CODE 供应商代码,SUPPLIER_NAME 供应商名称,ISNULL(PAYMENT_TERM.PAYMENT_TERM_CODE,'') 付款条件编号,ISNULL(PAYMENT_TERM.PAYMENT_TERM_NAME,'') 付款条件,CURRENCY.CURRENCY_NAME 币种,CAST(EXCHANGE_RATE AS REAL) 汇率,DOC_Sequence 传送次数,'' 正文内容,ISNULL(SUPPLIER_CONTACT.EMAIL,'') 收件人 FROM PURCHASE_ORDER LEFT JOIN SUPPLIER ON PURCHASE_ORDER.SUPPLIER_ID=SUPPLIER.SUPPLIER_BUSINESS_ID LEFT JOIN PAYMENT_TERM ON PAYMENT_TERM.PAYMENT_TERM_ID=PURCHASE_ORDER.PAYMENT_TERM_ID LEFT JOIN CURRENCY ON CURRENCY.CURRENCY_ID=PURCHASE_ORDER.CURRENCY_ID LEFT JOIN SUPPLIER_PURCHASE ON SUPPLIER_PURCHASE.SUPPLIER_ID=SUPPLIER.SUPPLIER_BUSINESS_ID LEFT JOIN SUPPLIER_CONTACT ON SUPPLIER_PURCHASE.SUPPLIER_BUSINESS_ID=SUPPLIER_CONTACT.SUPPLIER_BUSINESS_ID WHERE DOC_Sequence=0 AND PURCHASE_ORDER.ApproveStatus='Y' 
                     AND (SUPPLIER_NAME LIKE '%" + txtTH.Text + "%' OR SUPPLIER.SUPPLIER_CODE='" + txtTH.Text + "')";
            //txtTH.Text = strSql;
            try
            {
                DataTable dt;
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                gridControl1.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button19_Click(object sender, EventArgs e)
        {

            //if (comboBox2.Text.Trim() == "")
            //{
            //    MessageBox.Show("打印格式不可为空！", "系统提示");
            //    return;
            //}
            //string x1 = dataGridView1.CurrentRow.Cells["TD001"].Value.ToString();
            //string x2 = dataGridView1.CurrentRow.Cells["TD002"].Value.ToString();
            //string x3 = "";
            //string x4 = comboBox2.Text;
            //string x5 = "";
            //string x6 = "";
            //string x7 = "";
            //string x8 = "";
            //JYCX xx = new JYCX(x1, x2, x3, x4, x5, x6, x7, x8);
            //POPrint L = new POPrint();
            //L.cdssh = new POPrint.Cdssh(textBox2_TextChanged);
            //L.Show();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (comboBox2.Text == "")
            {
                MessageBox.Show("打印格式不可为空！", "系统提示");
                return;
            }
            DesignReport();
        }

        private void DesignReport()
        {
            if (FReportRunning)
                return;
            string thisFolder = Config.ApplicationFolder;
            DirectoryInfo theFolder = new DirectoryInfo(thisFolder + @"Reports\采购单\");
            string ReportName = theFolder + comboBox2.Text + ".frx";
            FReport = new Report();
            FReport.Load(ReportName);
            RegisterData();
            FReport.Design();
            //PreviewReport();
        }

        private void RegisterData()
        {
            //FReport.RegisterData(FDataSet, "NorthWind");
            //FReport.RegisterData(FBusinessObject, "Categories BusinessObject");
        }

        private void PreviewReport()
        {
            if (FReportRunning)
                return;

            FReportRunning = true;
            try
            {
                RegisterData();
                FReport.Show();
            }
            finally
            {
                FReportRunning = false;
            }
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
        {
            //textBox8.Text = dateTimePicker3.Value.ToShortDateString();
        }

        private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
        {
            //textBox10.Text = dateTimePicker4.Value.ToShortDateString();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //if (comboBox4.Text != "未审核")
            //{
            //    MessageBox.Show("只有未审核的采购单才可以释放！", "软件提示");
            //    return;
            //}

            //foreach (DataGridViewRow dr in dataGridView1.Rows)
            //{
            //    if (Convert.ToBoolean(dr.Cells["colCD1"].Value))
            //    {
            //        String xmh = (dr.Cells["TC047"].Value == null) ? "" : dr.Cells["TC047"].Value.ToString().Trim();
            //        String yqwg = (dr.Cells["YQWGRQ"].Value == null) ? "" : dr.Cells["YQWGRQ"].Value.ToString().Trim() ;
            //        String jhwg = (dr.Cells["JHJQ"].Value == null) ? "" : dr.Cells["JHJQ"].Value.ToString().Trim();
            //        if (yqwg == "" && (xmh.IndexOf("A")>=0 || xmh.IndexOf("X") >= 0))
            //        {
            //            MessageBox.Show("要求完工日期为空不能释放采购单！", "系统提示");
            //            return;
            //        }
            //        if (jhwg== "" && (xmh.IndexOf("A") >= 0 || xmh.IndexOf("X") >= 0))
            //        {
            //            MessageBox.Show("计划完工日期为空不能释放采购单！", "系统提示");
            //            return;
            //        }
            //    }
            //}

            //        string strSql = "";
            //foreach (DataGridViewRow dr in dataGridView1.Rows)
            //{

            //    if (dr.Index <= dataGridView1.Rows.Count - 1)
            //    {
            //        if (Convert.ToBoolean(dr.Cells["colCD1"].Value))
            //        {

            //                strSql = "UPDATE PURTD SET TD018='Y' WHERE TD001='" + dr.Cells["TD001"].Value + "' AND TD002='" + dr.Cells["TD002"].Value + "' AND TD003='" + dr.Cells["TD003"].Value + "'";

            //                try
            //                {
            //                    db = new DataBase();
            //                    if (db.ExecDataBySql1(strSql) > 0)
            //                    {
            //                    MessageBox.Show("采购订单释放成功！", "软件提示");
            //                }
            //                    else
            //                    {
            //                        MessageBox.Show("更新失败，请检查", "软件提示");
            //                        return;
            //                    }
            //                }
            //                catch (Exception ex)
            //                {
            //                    MessageBox.Show(ex.Message, "软件提示");
            //                    throw ex;
            //                }
            //            strSql = "UPDATE PURTC SET TC014='Y' WHERE TC001='" + dr.Cells["TD001"].Value + "' AND TC002='" + dr.Cells["TD002"].Value + "'";

            //            try
            //            {
            //                db = new DataBase();
            //                if (db.ExecDataBySql1(strSql) > 0)
            //                {                            
            //                }
            //                else
            //                {
            //                }
            //            }
            //            catch (Exception ex)
            //            {
            //                MessageBox.Show(ex.Message, "软件提示");
            //                throw ex;
            //            }

            //        }
            //        }
            //}
            //button20.PerformClick();
            
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            //button20.PerformClick();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //string strSql = "";
            //strSql = "UPDATE PURTD SET UDF07='" + this.dataGridView1.CurrentCell.Value.ToString() + "'  WHERE TD001='" + dataGridView1["TD001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TD002='" + dataGridView1["TD002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TD003='" + dataGridView1["TD003", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";
            //db = new DataBase();
            //if (db.ExecDataBySql1(strSql) > 0)
            //{

            //}
            //else
            //{

            //}
        }

        private void button21_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = "SELECT '" + PropertyClass.OperatorCode.Trim() + "'+CONVERT(CHAR(8),GETDATE(),112)+ISNULL(RIGHT('00'+RTRIM(CAST(CAST(MAX(RIGHT(RTRIM(PURTO.UDF04),3)) AS NUMERIC(11))+1 AS CHAR(11))),3) ,'001') AS 单号 FROM PURTO WHERE PURTO.UDF04 LIKE '" + PropertyClass.OperatorCode.Trim() + "' + CONVERT(CHAR(8),GETDATE(),112)+'%'";
            try
            {
                sdr1 = db.GetDataReader1(strSql);

                while (sdr1.Read())
                {
                    textBox2.Text = sdr1["单号"].ToString();
                }

                if (sdr1.HasRows == false)
                {

                }

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                throw ex;
            }
            finally
            {
                sdr1.Close();
            }

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            //string strSql = null;
            //strSql = " SELECT TB060 指令号,PURTO.UDF04 询价批次号,TP004 供应商编号,MA002 供应商名称,TO001 询价单别,TO002 询价单号,TO003 询价日期,TP003 询价序号,TO004 品号,TO005 品名,TO006 规格,MB004 单位,INVMB.UDF01 型号,INVMB.UDF02 材质,PURTO.UDF01 请购单别,PURTO.UDF02 请购单号,TB009 数量,PURTO.UDF03 请购序号,TP007 是否含税,TP010 单价  FROM PURTO LEFT JOIN PURTP ON TO001=TP001 AND TO002=TP002 LEFT JOIN PURMA ON TP004=MA001 LEFT JOIN PURTB ON PURTO.UDF01=TB001 AND PURTO.UDF02=TB002 AND PURTO.UDF03=TB003 LEFT JOIN INVMB ON TO004=MB001 WHERE TP010=0 AND TB039='N' AND (MA002 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='')  AND ((ISNULL(PURTO.UDF04,'') = '" + textBox5.Text + "' OR '" + textBox5.Text + "'=''))  AND (MA047='" + PropertyClass.ygbh + "' OR '" + PropertyClass.jiaose + "'='计划员')";
            //try
            //{
            //    db = new DataBase();
            //    dataGridView7.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void button8_Click_2(object sender, EventArgs e)
        {
            //SaveFileDialog dlg = new SaveFileDialog();
            //dlg.Filter = "Execl files (*.xls)|*.xls";
            //dlg.FilterIndex = 0;
            //dlg.RestoreDirectory = true;
            //dlg.CreatePrompt = true;
            //dlg.Title = "保存为Excel文件";
            //if (dlg.ShowDialog() == DialogResult.OK)
            //{
            //    Stream myStream;
            //    myStream = dlg.OpenFile();
            //    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            //    string columnTitle = null;
            //    try
            //    {
            //        //写入列标题    
            //        for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //        {
            //            if (dataGridView1.Columns[i].Visible == true)
            //            {
            //                if (i > 0)
            //                {
            //                    columnTitle += "\t";
            //                }
            //                columnTitle += dataGridView1.Columns[i].HeaderText;

            //            }

            //        }
            //        sw.WriteLine(columnTitle);
            //        //写入列内容    
            //        for (int j = 0; j < dataGridView1.Rows.Count; j++)
            //        {

            //            string columnValue = null;
            //            for (int k = 0; k < dataGridView1.Columns.Count; k++)
            //            {
            //                if (dataGridView1.Columns[k].Visible == true)
            //                {
            //                    if (k > 0)
            //                    {
            //                        columnValue += "\t";
            //                    }
            //                    if (dataGridView1.Rows[j].Cells[k].Value == null)
            //                        columnValue += "";
            //                    else
            //                        columnValue += dataGridView1.Rows[j].Cells[k].Value.ToString().Trim();
            //                }
            //            }
            //            sw.WriteLine(columnValue);
            //        }
            //        sw.Close();
            //        myStream.Close();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.ToString());
            //    }
            //    finally
            //    {
            //        sw.Close();
            //        myStream.Close();
            //    }
            //}
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //string strSql = null;
            //strSql = " SELECT TB060 指令号,TP004 供应商编号,MA002 供应商名称,TO001 询价单别,TO002 询价单号,TO003 询价日期,TP003 询价序号,TO004 品号,TO005 品名,TO006 规格,MB004 单位,INVMB.UDF01 型号,INVMB.UDF02 材质,PURTO.UDF01 请购单别,PURTO.UDF02 请购单号,TB009 数量,PURTO.UDF03 请购序号,TP007 是否含税,TP010 单价  FROM PURTO LEFT JOIN PURTP ON TO001=TP001 AND TO002=TP002 LEFT JOIN PURMA ON TP004=MA001 LEFT JOIN PURTB ON PURTO.UDF01=TB001 AND PURTO.UDF02=TB002 AND PURTO.UDF03=TB003 LEFT JOIN INVMB ON TO004=MB001 LEFT JOIN PURTA ON TB001=TA001 AND TB002=TA002 WHERE TA003<=CONVERT(CHAR(8),GETDATE()-3,112) AND TB039='N' AND (MA002 LIKE '%" + textBox6.Text + "%' OR '" + textBox6.Text + "'='')  AND ((ISNULL(PURTO.UDF04,'') = '" + textBox5.Text + "' OR '" + textBox5.Text + "'=''))  AND (MA047='" + PropertyClass.ygbh + "' OR '" + PropertyClass.jiaose + "'='计划员')";
            //try
            //{
            //    db = new DataBase();
            //    dataGridView7.DataSource = db.GetDataSet1(strSql, "PURTB").Tables["PURTB"];
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}
        }

        private void button23_Click(object sender, EventArgs e)
        {
            string fileName = textBox6.Text;

            string saveFileName = "";

            SaveFileDialog saveDialog = new SaveFileDialog();

            saveDialog.DefaultExt = "xls";

            saveDialog.Filter = "Excel文件|*.xls";

            saveDialog.FileName = fileName;

            saveDialog.ShowDialog();

            saveFileName = saveDialog.FileName;

            if (saveFileName.IndexOf(":") < 0) return; //被点了取消

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {

                MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");

                return;

            }

            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;

            Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 

            //写入标题             

            for (int i = 0; i < dataGridView4.ColumnCount; i++)

            { worksheet.Cells[1, i + 1] = dataGridView4.Columns[i].HeaderText; }

            //写入数值

            for (int r = 0; r < dataGridView4.Rows.Count; r++)
            {
                for (int i = 0; i < dataGridView4.ColumnCount; i++)
                {

                    worksheet.Cells[r + 2, i + 1] = dataGridView4.Rows[r].Cells[i].Value;

                }

                System.Windows.Forms.Application.DoEvents();

            }

            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

            MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);

            if (saveFileName != "")
            {

                try
                {
                    workbook.Saved = true;

                    workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 

                }

                catch (Exception ex)
                {//fileSaved = false;                      

                    MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);

                }

            }

            xlApp.Quit();

            GC.Collect();//强行销毁       
        }

        private void rowMergeView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.rowMergeView1.Controls.Clear();//移除所有控件 
            if (e.ColumnIndex.Equals(this.rowMergeView1.Columns["ZBGYS"].Index))
  
            {
                System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                btn.Text = "...";//设置button文字    
                btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                btn.Visible = true;//设置控件允许显示    

                btn.Width = this.rowMergeView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                btn.Height = this.rowMergeView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

                this.rowMergeView1.Controls.Add(btn);//dataGridView1中添加控件btn    

                btn.Location = new System.Drawing.Point(((this.rowMergeView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                        (btn.Width)), this.rowMergeView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            int x3 = 0;
            string x4 = "";
            string x5 = "";
            x1 = rowMergeView1["TB001", rowMergeView1.CurrentCell.RowIndex].Value.ToString();
            x2 = rowMergeView1["TB002", rowMergeView1.CurrentCell.RowIndex].Value.ToString();
            x3 = Convert.ToInt32(rowMergeView1["TB009", rowMergeView1.CurrentCell.RowIndex].Value.ToString());
            x5 = rowMergeView1["TB003", rowMergeView1.CurrentCell.RowIndex].Value.ToString();
            x4 = "查看询价单";
            MXCX xx = new MXCX(x1, x2, x3, x4, x5);
            k = new CHAXUN();
            k.DataSelected += new EventHandler(k_DataSelected);
            k.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
            k.Show(this);
        }

        void k_DataSelected(object sender, EventArgs e)
        {

            //this.rowMergeView1.CurrentCell.Value = k.clph;
            //this.rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["ZMA002"].Index].Value = k.clpm;
            //this.rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["ZBDJ"].Index].Value = k.cldj;
            //string strSql = "";
            //string xjdb = (rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP001"].Index].Value == null) ? "" : rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP001"].Index].Value.ToString();
            //string xjdh = (rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP002"].Index].Value == null) ? "" : rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP002"].Index].Value.ToString();
            //string xjxh = (rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP003"].Index].Value == null) ? "" : rowMergeView1.Rows[this.rowMergeView1.CurrentCell.RowIndex].Cells[this.rowMergeView1.Columns["TP003"].Index].Value.ToString();
            //strSql = "UPDATE PURTP SET TP015='N' WHERE TP001='" + xjdb + "' AND TP002='" + xjdh + "'";
            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}

            //strSql = "UPDATE PURTP SET TP015='Y' WHERE TP001='" + xjdb + "' AND TP002='" + xjdh + "' AND TP004='" + k.clph + "'";
            //try
            //{
            //    if (db.ExecDataBySql1(strSql) > 0)
            //    {

            //    }
            //    else
            //    {

            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "软件提示");
            //    throw ex;
            //}

        }

        private void textBox7_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button6.PerformClick();
                string strSql = "";
                strSql = "select RTRIM(UDF01) AS UDF01 FROM PURMA  WHERE MA001='" + textBox7.Text + "' AND ISNULL(UDF01,'')<>''";
                strSql += "UNION ALL select UDF02 FROM PURMA  WHERE MA001='" + textBox7.Text + "' AND ISNULL(UDF02,'')<>''";
                strSql += "UNION ALL select UDF03 FROM PURMA  WHERE MA001='" + textBox7.Text + "' AND ISNULL(UDF02,'')<>''";
                try
                {
                    System.Data.DataTable dt;
                    dt = db.GetDataSet1(strSql, "PURMA").Tables["PURMA"];
                    comboBox3.ValueMember = "UDF01";
                    comboBox3.DisplayMember = "UDF01";
                    comboBox3.DataSource = dt;
                    comboBox3.SelectedIndex = 0;
                }
                catch
                {
                }
            }
        }

        private void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                Double xdts = (this.dataGridView4.Rows[e.RowIndex].Cells[this.dataGridView4.Columns["已下单天数"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView4.Rows[e.RowIndex].Cells[this.dataGridView4.Columns["已下单天数"].Index].Value.ToString());

                if (xdts > 3)
                {

                    for (int i = 0; i <= this.dataGridView4.Columns.Count - 1; i++)
                    {
                        this.dataGridView4.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Yellow;
                    }
                }
            }
            catch
            {

            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //Double xdts = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["DJS"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["DJS"].Index].Value.ToString());

            //if (xdts <= 3)
            //{

            //    for (int i = 0; i <= this.dataGridView1.Columns.Count - 1; i++)
            //    {
            //        this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Yellow;
            //    }
            //}
        }

        private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button24_Click(object sender, EventArgs e)
        {
            //foreach (DataGridViewRow dr in dataGridView1.Rows)
            //{

            //    if (dr.Index <= dataGridView1.Rows.Count - 1)
            //    {
            //        if (Convert.ToBoolean(dr.Cells["colCD1"].Value))
            //        {
            //            //PURTD -- UDF05
            //            //string strSql = "UPDATE MOCTB SET UDF01=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker5.Value.ToShortDateString() + "'),112) FROM MOCTA WHERE TB001=TA001 AND TB002=TA002 AND TA083 LIKE '" + dr.Cells["TC047"].Value.ToString().Trim() + "%' AND TB003='" + dr.Cells["TD004"].Value + "'";
            //            string strSql = "UPDATE PURTD SET UDF05=CONVERT(CHAR(8),DATEADD(DAY,0,'" + dateTimePicker5.Value.ToShortDateString() + "'),112) WHERE TD001='" + dr.Cells["TD001"].Value.ToString().Trim() + "' AND TD002='" + dr.Cells["TD002"].Value.ToString().Trim() + "' AND TD003 = '" + dr.Cells["TD003"].Value.ToString().Trim() + "'";

            //            try
            //            {
            //                db = new DataBase();
            //                if (db.ExecDataBySql1(strSql) > 0)
            //                {
                               
            //                }
            //                else
            //                {
                               
            //                }
            //            }
            //            catch (Exception ex)
            //            {
            //                MessageBox.Show(ex.Message, "软件提示");
            //                throw ex;
            //            }
            //        }
            //    }
            //}
            //MessageBox.Show("修改成功！", "软件提示");
            //button20.PerformClick();
        }

        private void 发起内部联络单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
        //    _XMH.Clear();
        //    _GDDB.Clear();
        //    _GDDH.Clear();
        //    _CPPH.Clear();
        //    _CPPM.Clear();
        //    _CPGG.Clear();
        //    _CPSL.Clear();
        //    _JHWG.Clear();
        //    _YJWG.Clear();
        //    _YQDL.Clear();
        //    _ZZQRJQA.Clear();
        //    for (int i = 0; i <= this.dataGridView1.Rows.Count-1; i++)
        //    {
        //        if (Convert.ToBoolean(this.dataGridView1.Rows[i].Cells["colCD1"].Value))
        //        {

        //            _XMH.Add(dataGridView1.Rows[i].Cells["TC047"].Value.ToString());
        //            _GDDB.Add(dataGridView1.Rows[i].Cells["TD001"].Value.ToString());
        //            _GDDH.Add(dataGridView1.Rows[i].Cells["TD002"].Value.ToString());
        //            _CPPH.Add(dataGridView1.Rows[i].Cells["TD004"].Value.ToString());
        //            _CPPM.Add(dataGridView1.Rows[i].Cells["TD005"].Value.ToString());
        //            _CPGG.Add(dataGridView1.Rows[i].Cells["TD006"].Value.ToString());
        //            _CPSL.Add(dataGridView1.Rows[i].Cells["TD008"].Value.ToString());
        //            _JHWG.Add(dataGridView1.Rows[i].Cells["JHJQ"].Value.ToString());
        //            _YJWG.Add(dataGridView1.Rows[i].Cells["TD012"].Value.ToString());
        //            _YQDL.Add(dataGridView1.Rows[i].Cells["TD012"].Value.ToString());
        //            _ZZQRJQA.Add(dataGridView1.Rows[i].Cells["ZZQRJQ"].Value.ToString());
        //            _ZJPH.Add("");
        //            _ZJPM.Add("");
        //        }
        //}
        //    _GZZX = "09";
        //    _ZXMC = "采购";
        //    GDlist xx = new GDlist(_XMH, _GDDB, _GDDH, _CPPH, _CPPM, _CPGG, _CPSL, _GZZX, _ZXMC, _JHWG, _YJWG, _YQDL, _ZZQRJQA,_ZJPH,_ZJPM);
        //    OAlink g = new OAlink();
        //    g.cdgzzx = new OAlink.Cdgzzx(textBox2_TextChanged);
        //    g.Show();
        }

        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {

        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                gridControl1.DataSource = null;
                string strSql = "";
                String cgdh = this.gridView1.GetFocusedRowCellValue("采购单号").ToString();
                //String gybh = this.gridView2.GetFocusedRowCellValue("工艺路线编号").ToString();
                strSql = "SELECT A.SequenceNumber 序号,C.ITEM_CODE 品号,C.ITEM_NAME 品名,C.ITEM_SPECIFICATION 规格,CAST(A.BUSINESS_QTY AS REAL) 订单数量,RTRIM(A.UDF021) 客户单号,CONVERT(CHAR(10),A.PLAN_ARRIVAL_DATE,111) 预交货日期,A.REMARK 备注 FROM PURCHASE_ORDER_D A LEFT JOIN PURCHASE_ORDER B ON A.PURCHASE_ORDER_ID=B.PURCHASE_ORDER_ID LEFT JOIN ITEM C ON C.ITEM_BUSINESS_ID=A.ITEM_ID WHERE B.DOC_NO='" + cgdh + "' ORDER BY A.SequenceNumber";
                DataTable dt;
                db = new DataBase();
                dt = db.GetDataSet1(strSql, "MOCTB").Tables["MOCTB"];
                gridControl2.DataSource = dt;
            }
            catch
            {

            }
        }
    }
}

                


